package pl.druminski.robinhood.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import pl.druminski.robinhood.model.Data;
import pl.druminski.robinhood.model.PluginTableModel;
import pl.druminski.robinhood.model.ProxyTableModel;
import pl.druminski.robinhood.model.UpdatingInfo;

/**
 * @author Łukasz Drumiński
 * 
 */
public class Gui extends JFrame implements ActionListener
{

    private Data data;
    private JTabbedPane tabs;
    private JPanel pnlBasic = new JPanel();
    private JPanel pnlProxy = new JPanel();
    private JPanel pnlAbout = new JPanel();
    private JPanel pnlPlugin = new JPanel();
    private JPanel pnlUpdate = new JPanel();
    private JPanel pnlButtons = new JPanel();
    private JCheckBox chckBoxUpdate;
    public JCheckBox chckBoxAutostart;
    private JRadioButton[] btnsUpdate;
    private JLabel lblUpdate;
    public JTable tblProxy;
    public ProxyTableModel proxyModel;
    public PluginTableModel pluginModel;
    public JTable tblPlugin;
    public JButton btnAddProxy = new JButton("Dodaj");
    public JButton btnDeleteProxy = new JButton("Usuń");
    public JButton btnSave = new JButton("Zapisz");
    public JTextField fldStartTime = new JTextField("0");
    public JCheckBox chckExit;
    public JTextField fldExitTime = new JTextField("0");
    public JLabel lblExit;


    public Gui(Data data)
    {
        this.data = data;

        setLayout(new BorderLayout());
        setTitle("Robin Hood Opcje");

        tabs = new JTabbedPane();
        JButton btnCancel = new JButton("Anuluj");
        btnCancel.setActionCommand("cancel");
        btnCancel.addActionListener(this);

        pnlButtons.add(btnSave);
        pnlButtons.add(btnCancel);

        //dodawanie elementow do interfejsu
        createPanelWithUpdate();
        createPanelWithAutostart();
        createPanelWithSymulationInfo();
        createPanelProxy();
        createPanelPlugin();
        createPanelAbout();

        tabs.addTab("Podstawowe", pnlBasic);
        tabs.addTab("Proxy", pnlProxy);
        tabs.add("Wtyczki", pnlPlugin);
        tabs.addTab("O Programie", pnlAbout);
        this.setPreferredSize(new Dimension(600, 400));
        add(tabs);
        add(pnlButtons, BorderLayout.SOUTH);
        pack();

        //ustaw rame na srodku pulpitu
        setLocationRelativeTo(null);
        setVisible(true);

    }

    private void createPanelPlugin()
    {
        pluginModel = new PluginTableModel(data);
        tblPlugin = new JTable(pluginModel);
        JScrollPane scrollPane = new JScrollPane(tblPlugin);
        tblPlugin.getColumnModel().getColumn(0).setHeaderValue("Nazwa");
        tblPlugin.getColumnModel().getColumn(1).setHeaderValue("Wczytuj");
        tblPlugin.getColumnModel().getColumn(2).setHeaderValue("Ilość kliknięć");
        tblPlugin.getColumnModel().getColumn(3).setHeaderValue("Przekazana kwota(zł)");
        tblPlugin.getColumnModel().getColumn(4).setHeaderValue("Wiek (w dniach)");

        pnlPlugin.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;

        pnlPlugin.add(scrollPane, c);

    }

    private void createPanelAbout()
    {
        JPanel pnl = new JPanel();
        JPanel pnl3 = new JPanel();
        JPanel pnl2 = new JPanel();

        pnl2.setLayout(new BoxLayout(pnl2, BoxLayout.Y_AXIS));
        pnl3.setLayout(new BoxLayout(pnl3, BoxLayout.X_AXIS));
        pnl3.add(new JLabel("Strona: "));
        pnl3.add(new HyperLinkLabel("www.druminski.ovh.org", "www.druminski.ovh.org"));
        pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));

        pnl2.add(new JLabel("Program: RobinHood"));
        pnl2.add(new JLabel("Wersja: " + data.getVersion()));
        pnl2.add(new JLabel("Autor: Łukasz Drumiński"));
        pnl2.add(new JLabel("Kontakt: druminski@gmail.com"));
        pnl2.setAlignmentX(CENTER_ALIGNMENT);
        pnl.add(pnl2);
        pnl.add(pnl3);
       
        pnlAbout.add(Box.createRigidArea(new Dimension(0, 250)), BorderLayout.NORTH);
        pnlAbout.add(pnl, BorderLayout.CENTER);
    }

    private void createPanelProxy()
    {
        proxyModel = new ProxyTableModel(data);

        tblProxy = new JTable(proxyModel);
        tblProxy.getColumnModel().getColumn(0).setHeaderValue("Adres IP");
        tblProxy.getColumnModel().getColumn(1).setHeaderValue("Port");
        tblProxy.getColumnModel().getColumn(2).setHeaderValue("Typ");

        JScrollPane scrollPane = new JScrollPane(tblProxy);
        JPanel pnl = new JPanel(new BorderLayout());

        tblProxy.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblProxy.setRowSelectionAllowed(true);
        tblProxy.setColumnSelectionAllowed(false);

        btnAddProxy = new JButton("Dodaj");
        btnDeleteProxy = new JButton("Usuń");
        JLabel lbl = new JLabel("Im więcej proxy,");
        JLabel lbl2 = new JLabel("tym więcej kliknięć!");
        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
        lbl2.setAlignmentX(Component.CENTER_ALIGNMENT);
        btnAddProxy.setAlignmentX(Component.CENTER_ALIGNMENT);
        btnDeleteProxy.setAlignmentX(Component.CENTER_ALIGNMENT);
        pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));

        pnl.add(Box.createRigidArea(new Dimension(0, 10)), BorderLayout.NORTH);
        pnl.add(lbl, BorderLayout.NORTH);
        pnl.add(lbl2, BorderLayout.NORTH);
        pnl.add(Box.createRigidArea(new Dimension(0, 10)), BorderLayout.NORTH);
        pnl.add(btnAddProxy, BorderLayout.NORTH);
        pnl.add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);
        pnl.add(btnDeleteProxy, BorderLayout.NORTH);

        pnlProxy.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;

        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.8;
        c.gridheight = 3;
        c.gridwidth = 2;
        c.weighty = 1;

        pnlProxy.add(scrollPane, c);

        c.weightx = 0.2;
        c.weighty = 1;
        c.gridx = 2;
        c.gridy = 0;
        c.gridheight = 3;
        c.gridwidth = 1;

        pnlProxy.add(pnl, c);

    }

    private void createPanelWithSymulationInfo()
    {
        JPanel pnl1 = new JPanel();
        JPanel pnl2 = new JPanel();
        JPanel pnl3 = new JPanel();
        JPanel pnl4 = new JPanel();
        
        pnl1.setBorder(BorderFactory.createTitledBorder(" Inne akcje "));
        pnl1.setLayout(new BoxLayout(pnl1, BoxLayout.Y_AXIS));
        pnl1.setPreferredSize(new Dimension(580, 100));

        JLabel lbl1 = new JLabel(" Po ilu minutach od startu programu ma zostać przeprowadzona symulacja kliknięć?");
        lblExit = new JLabel(" Po ilu minutach od zakończenia symulacji kliknięć program ma się wyłączyć?");
        chckExit = new JCheckBox("Program ma się wyłączać po przeprowadzeniu symulacji kliknięć?");

        fldStartTime.setColumns(3);
        fldExitTime.setColumns(3);
        
        chckExit.setAlignmentX(Component.LEFT_ALIGNMENT);

        pnl2.setLayout(new BorderLayout());
        pnl2.add(fldStartTime, BorderLayout.LINE_START);
        pnl2.add(lbl1);
        
        pnl3.setLayout(new BorderLayout());
        pnl3.add(fldExitTime, BorderLayout.LINE_START);
        pnl3.add(lblExit);
        
        pnl4.setLayout(new BorderLayout());
        pnl4.add(chckExit);

        pnl1.setAlignmentX(LEFT_ALIGNMENT);
        pnl1.add(pnl2);
        pnl1.add(pnl4);
        pnl1.add(pnl3);
        chckExit.addActionListener(this);
        chckExit.setActionCommand("exitTime");

        pnlBasic.add(pnl1);

        if (data.getExitTime() == -1) 
        {
            chckExit.setSelected(false);
            setEnableExit(false);
        }
        else
        {
            chckExit.setSelected(true);
            fldExitTime.setText("" + data.getExitTime());
        }

        fldStartTime.setText("" + data.getStartTime());


    }

    private void createPanelWithAutostart()
    {
        JPanel pnl1 = new JPanel();
        pnl1.setBorder(BorderFactory.createTitledBorder(" Autostart (Windows) "));
        pnl1.setLayout(new BoxLayout(pnl1, BoxLayout.Y_AXIS));
        pnl1.setPreferredSize(new Dimension(580, 50));

        chckBoxAutostart = new JCheckBox("Uruchamiaj program wraz ze startem systemu operacyjnego.");
        chckBoxAutostart.setSelected(data.getRunWithSystemStart());
        pnl1.add(chckBoxAutostart);

        pnlBasic.add(pnl1);
    }

    private void createPanelWithUpdate()
    {
        //Panel dla opcji z aktualizacjami
        JPanel pnl1 = new JPanel();
        pnl1.setBorder(BorderFactory.createTitledBorder(" Aktualizacje "));
        pnl1.setLayout(new BoxLayout(pnl1, BoxLayout.Y_AXIS));
        pnl1.setPreferredSize(new Dimension(580, 120));

        chckBoxUpdate = new JCheckBox("Automatycznie sprawdzaj czy dostępne są aktualizacje.");
        chckBoxUpdate.addActionListener(this);
        chckBoxUpdate.setActionCommand("update");
        pnl1.add(chckBoxUpdate);

        lblUpdate = new JLabel("Kiedy odnalezione zostaną aktualizacje dla programu Robin Hood: ");
        ButtonGroup group = new ButtonGroup();
        btnsUpdate = new JRadioButton[2];

        btnsUpdate[0] = new JRadioButton("zapytaj, co robić");
        btnsUpdate[0].setActionCommand("manual");
        btnsUpdate[0].addActionListener(this);

        btnsUpdate[1] = new JRadioButton("automatycznie pobierz i zainstaluj aktualizacje");
        btnsUpdate[1].setActionCommand("auto");
        btnsUpdate[1].addActionListener(this);

        group.add(btnsUpdate[0]);
        group.add(btnsUpdate[1]);

        pnlUpdate.setLayout(new BoxLayout(pnlUpdate, BoxLayout.Y_AXIS));
        pnlUpdate.add(lblUpdate);
        pnlUpdate.add(btnsUpdate[0]);
        pnlUpdate.add(btnsUpdate[1]);

        pnl1.add(pnlUpdate, BorderLayout.SOUTH);

        pnlBasic.add(pnl1);

        if (data.getUpdatingMethod() != UpdatingInfo.NONE)
        {
            chckBoxUpdate.doClick();
            if (data.getUpdatingMethod().equals(UpdatingInfo.MANUAL))
            {
                btnsUpdate[0].doClick();
            } else
            {
                btnsUpdate[1].doClick();
            }
        }
    }

    public void setEnableUpdate(Boolean enable)
    {
        btnsUpdate[0].setEnabled(enable);
        btnsUpdate[1].setEnabled(enable);
        lblUpdate.setEnabled(enable);
    }

    public void setEnableExit(Boolean enable)
    {
        fldExitTime.setEnabled(enable);
        lblExit.setEnabled(enable);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("update"))
        {
            if (chckBoxUpdate.isSelected())
            {
                setEnableUpdate(true);
            } else
            {
                setEnableUpdate(false);
                data.setUpdatingMethod(UpdatingInfo.NONE);

            }
        } else if (e.getActionCommand().equals("auto"))
        {
            data.setUpdatingMethod(UpdatingInfo.AUTO);
        } else if (e.getActionCommand().equals("manual"))
        {
            data.setUpdatingMethod(UpdatingInfo.MANUAL);
        } else if (e.getActionCommand().equals("cancel"))
        {
            //zamknij okno
            this.dispose();
        } else if (e.getActionCommand().equals("exitTime"))
        {
            if (chckExit.isSelected())
            {
                setEnableExit(true);
                String tmp = fldExitTime.getText();
                if (!tmp.equals(""))
                {
                    int t = Integer.parseInt(tmp);
                    if (t < 0) t = 0;
                    data.setExitTime(t);
                }
            } else
            {
                setEnableExit(false);
                data.setExitTime(-1);
            }
        }
    }
}
